package com.freewater.springboot.demo.dataaccess.transaction.listener;

import com.freewater.springboot.demo.dataaccess.transaction.listener.entity.Order;
import com.freewater.springboot.demo.dataaccess.transaction.listener.entity.OrderItem;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

public class Main {
    public static void main(String[] args) {
        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();
        applicationContext.register(AppConfig.class);
        applicationContext.refresh();
        OrderService orderService = applicationContext.getBean(OrderService.class);
        Order order = new Order();
        order.setId(1L);
        order.setUserId("10001");
        order.setCount(10);
        order.setAmount(new BigDecimal(100));
        order.setStatus(0);
        order.setVersion(0);
        //
        List<OrderItem> orderItems = new ArrayList<>();
        order.setOrderItems(orderItems);
        OrderItem orderItem = new OrderItem();
        orderItem.setId(1L);
        orderItem.setOrderId(order.getId());
        orderItem.setProductId(1L);
        orderItem.setQuantity(10);
        orderItem.setUnitPrice(new BigDecimal(10));
        orderItem.setAmount(new BigDecimal(100));
        orderItems.add(orderItem);
        //orderService.createOrder(order);
        // 测试事务回滚，事务性事件监听器不会被触发
        orderService.createOrderWithError(order);
        Order savedOrder = orderService.findOrderById(order.getId());
        System.out.println(savedOrder);
    }
}
